package TruongNhaLib;

import java.text.DecimalFormat;
import java.util.List;

import www.truongnha.com.R;
import www.truongnha.com.SubmitMark;
import android.app.Activity;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;


public class MarkAdapter extends ArrayAdapter<MarkModel> {
	private final List<MarkModel> list;
	private final Activity context;
	private final int startPosition;
	private final int endPosition;
	private final int size = MarkModel.size_full;// 28
	private DecimalFormat format = new DecimalFormat("#.#");
	private static final Double[] marks = new Double[] {
        0.0, 0.5, 1.0, 1.5, 2.0, 2.5,3.0, 3.5,4.0, 4.5,5.0, 5.5,6.0, 6.5 ,7.0,7.5,8.0,8.5,9.0,9.5,10.0
    };
	private final ArrayAdapter<Double> adapterDouble = 
	        new ArrayAdapter<Double>( getContext(), R.layout.suggest_popup_list_item, marks);


	public MarkAdapter(Activity context, List<MarkModel> list, int startPosition, int endPosition) {
		super(context, R.layout.mark_item, list);
		this.context = context;
		this.list = list;
		this.startPosition = startPosition ;
		this.endPosition = endPosition;
		//show editable list to test:
		StringBuilder out = new StringBuilder("editable: ");
		for(int i =  0 ; i < list.size()  ; i++ ){
			//if(list.get(i).editableIth(startPosition))
				out.append(i +" ");
		}
		Log.d("test edit", out.toString());
	}
	static class ViewHolder {
		protected TextView name;
		protected AutoCompleteTextView mark_1;
		protected AutoCompleteTextView mark_2;
		protected AutoCompleteTextView mark_3;
		protected AutoCompleteTextView mark_4;
		protected AutoCompleteTextView mark_5;
		protected AutoCompleteTextView mark_6;
		protected AutoCompleteTextView mark_7;
		protected AutoCompleteTextView mark_8;
	}
	//show mark to test
	private void showMarkModel() {
		// TODO Auto-generated method stub
		StringBuilder output = new StringBuilder();
		for(int i = 0; i < 18 ; i++ ){
			Log.d("show", "i =" +i);
			Log.d("show", String.valueOf(list.get(i).getDiem(startPosition)) +"|"
						+String.valueOf(list.get(i).getDiem(startPosition +1)) +"|" 
						+String.valueOf(list.get(i).getDiem(startPosition +2)) +"|"
						+String.valueOf(list.get(i).getDiem(startPosition +3)) +"|");

			output.append("i=" + i+"-");
			output.append( String.valueOf(list.get(i).getDiem(startPosition)) ).append("|");
			output.append( String.valueOf(list.get(i).getDiem(startPosition + 1)) ).append("|");
			output.append( String.valueOf(list.get(i).getDiem(startPosition + 2)) ).append("|");
			output.append( String.valueOf(list.get(i).getDiem(startPosition + 3)) ).append("|");
			output.append("**");
		}
		//Log.d("markmodel", output.toString());		
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			Log.d("getView", startPosition + "getView");
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.mark_item, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.name = (TextView) view.findViewById(R.id.name);
			viewHolder.mark_1 = (AutoCompleteTextView) view.findViewById(R.id.mark_1);
			viewHolder.mark_1.setAdapter(adapterDouble);
			viewHolder.mark_2 = (AutoCompleteTextView) view.findViewById(R.id.mark_2);
			viewHolder.mark_2.setAdapter(adapterDouble);
			viewHolder.mark_3 = (AutoCompleteTextView) view.findViewById(R.id.mark_3);
			viewHolder.mark_3.setAdapter(adapterDouble);
			viewHolder.mark_4 = (AutoCompleteTextView) view.findViewById(R.id.mark_4);
			viewHolder.mark_4.setAdapter(adapterDouble);
			viewHolder.mark_5 = (AutoCompleteTextView) view.findViewById(R.id.mark_5);
			viewHolder.mark_5.setAdapter(adapterDouble);
			viewHolder.mark_6 = (AutoCompleteTextView) view.findViewById(R.id.mark_6);
			viewHolder.mark_6.setAdapter(adapterDouble);
			viewHolder.mark_7 = (AutoCompleteTextView) view.findViewById(R.id.mark_7);
			viewHolder.mark_7.setAdapter(adapterDouble);
			viewHolder.mark_8 = (AutoCompleteTextView) view.findViewById(R.id.mark_8);
			viewHolder.mark_8.setAdapter(adapterDouble);

			viewHolder.mark_1.setOnFocusChangeListener(new OnFocusChangeListener() {				
				public void onFocusChange(View v, boolean hasFocus) {
					// TODO Auto-generated method stub
					//This mean that: an editText with focus have changed from focus to no focus.
					//In this case, we need to update value of this editText to correlative markModel.
					if(!hasFocus){
						Log.d("focus changed", "You leaved " + viewHolder.mark_1.getText().toString());
						MarkModel element = (MarkModel) viewHolder.mark_1.getTag();
						Log.d("startPosition", startPosition + " ");
						String markValue = viewHolder.mark_1.getText().toString();
						if(!markValue.equals("")){
							Double diem = Double.valueOf(format.format(Double.valueOf(markValue)));
							if(diem >=0 && diem <= 10){//validate the mark : 0.0 --> 10.0
								Log.d("new diem = ", diem +"" );
								if(diem == 10.0)
									viewHolder.mark_1.setText("10");//chinh lai edittext theo dung format.
								else
									viewHolder.mark_1.setText(diem+"");//chinh lai edittext theo dung format.
								element.setDiem( new Diem(diem, true, startPosition) , startPosition);
								//submitAMark immediately
								((SubmitMark) context).submitMark( element.getDiemStringToSubmit(startPosition) );
								
							} else{//invalid mark, -> edittext = blank, model add (-1)
								element.setDiem( new Diem(-1.0, true, startPosition) , startPosition);							
								viewHolder.mark_1.setText("");
							}
						} else{
							element.setDiem( new Diem(-1.0, true, startPosition) , startPosition);							
						}
						//showMarkModel(); test
					} else{
						//mQuickAction.show(v); //This is quick action to enter mark. Implement later.
					}//end if.
				}
			});
			view.setTag(viewHolder);
			viewHolder.mark_1.setTag(list.get(position));
//			viewHolder.mark_2.setFilters(new InputFilter[]{filter});
/*			viewHolder.mark_2.setTag(list.get(position));
			viewHolder.mark_3.setTag(list.get(position));
			viewHolder.mark_4.setTag(list.get(position));
*/
			//viewHolder.checkbox.setTag(list.get(position));
		} else {
//			Log.d("getView", startPosition + "use converView");
			view = convertView;
			((ViewHolder) view.getTag()).mark_1.setTag(list.get(position));
/*			((ViewHolder) view.getTag()).mark_2.setTag(list.get(position));
			((ViewHolder) view.getTag()).mark_3.setTag(list.get(position));
			((ViewHolder) view.getTag()).mark_4.setTag(list.get(position));
*/
			//((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
		}
		
		final ViewHolder holder = (ViewHolder) view.getTag();
		holder.name.setText( String.format("%2d. %s",list.get(position).getIndex() , list.get(position).getName() ) );
		int curPosition = startPosition;
		double diem;
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition);
			if(diem >=0 && diem <=10)
				holder.mark_1.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition)){
				Log.d("editable", "false");
				holder.mark_1.setFocusable(false);
			} else{
				holder.mark_1.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition+1);
			if(diem >=0 && diem <=10)
				holder.mark_2.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition+1)){
				Log.d("editable", "false");
				holder.mark_2.setFocusable(false);
			} else{
				holder.mark_2.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition+2);
			if(diem >=0 && diem <=10)
				holder.mark_3.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition+2)){
				holder.mark_3.setFocusable(false);
				Log.d("editable", "false");
			} else{
				holder.mark_3.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition+3);
			if(diem >=0 && diem <=10)
				holder.mark_4.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition+3)){
				Log.d("editable", "false");
				holder.mark_4.setFocusable(false);
			} else{
				holder.mark_4.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition+4);
			if(diem >=0 && diem <=10)
				holder.mark_5.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition+4)){
				Log.d("editable", "false");
				holder.mark_5.setFocusable(false);
			} else{
				holder.mark_5.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition+5);
			if(diem >=0 && diem <=10)
				holder.mark_6.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition+5)){
				Log.d("editable", "false");
				holder.mark_6.setFocusable(false);
			} else{
				holder.mark_6.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition+6);
			if(diem >=0 && diem <=10)
				holder.mark_7.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition+6)){
				Log.d("editable", "false");
				holder.mark_7.setFocusable(false);
			} else{
				holder.mark_7.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		if(curPosition <= endPosition){
			diem = list.get(position).getDiem(startPosition+7);
			if(diem >=0 && diem <=10)
				holder.mark_8.setText( String.valueOf( diem ) );
			if( !list.get(position).editableIth(startPosition+7)){
				Log.d("editable", "false");
				holder.mark_8.setFocusable(false);
			} else{
				holder.mark_8.setTextColor(Color.RED);
			}
			curPosition++;
		}
		
		return view;
	}
}